package com.tensequare.sms.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.tensequare.sms.utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/27 16:34
 */
@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;
    @Value("${aliyun.sms.template_code}")
    private String template_code;
    @Value("${aliyun.sms.sign_name}")
    private String sign_name;
    //向阿里大于发送短信
    @RabbitHandler
    public void sendCode(Map map){
        try {
            Map data = new HashMap();
            data.put("code",map.get("code"));
            smsUtil.sendSms(map.get("mobile").toString(),template_code,sign_name,
                    JSON.toJSONString(data));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
